import 'package:enterprise_v6_app/app/components/custom_modele/custom_gradient_line.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_event_list_entity.dart';
import 'package:enterprise_v6_app/app/data/entities/application/emergency/emergency_event_plan_entity.dart';

/// 处置方案
class DisposalMode extends StatefulWidget {
  const DisposalMode({super.key});

  @override
  State<DisposalMode> createState() => _DisposalModeState();
}

class _DisposalModeState extends State<DisposalMode> {


  /// 事故事件数据
  EmergencyEventListEntity item = Get.arguments as EmergencyEventListEntity;

  /// 处置方案数据
  EmergencyEventPlanEntity disposalModeData = EmergencyEventPlanEntity();

  // 获取处置方案数据
  void getDisposalModeData() async {
    final res = await HttpUtil().generateGetRequest<EmergencyEventPlanEntity>(
      Interface.getAccidentInfoPlan,
      queryParameters: {
        'id': item.id,
      },
    ).execute();
    res.onSuccess((result) {
      if (result.data != null) {
        disposalModeData = result.data!;
        if(mounted) {
          setState(() {});
        }
      }
    });
  }


  @override
  void initState() {
    super.initState();
    getDisposalModeData();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
          children: [
            Container(
              height: AppDimens.dp12,
              color: AppColors.bg_color_grey,
            ),
            Container(
              height: AppDimens.dp2,
              color: AppColors.blue_button,
            ),
            Container(
              height: AppDimens.dp40,
              width: AppDimens.sw,
              color: const Color(0xFFF4F7FF),
              alignment: Alignment.centerLeft,
              child: Container(margin: EdgeInsets.only(left: AppDimens.dp40), child: Text('事故名称：${item.accidentName}', style: AppTextStyles.black_16)),
            ),
            Expanded(
              child: SingleChildScrollView(
                child: Column(
                  children: [
                    _buildBasicInfoItem('事故发生时间', disposalModeData.accidentAtTime, false,),
                    _buildBasicInfoItem('预案名称', disposalModeData.handlePlanName, true),
                    _buildBasicInfoItem('应急物资', disposalModeData.rescueGoods, true),
                    _buildBasicInfoItem('应急队伍', disposalModeData.rescueTeam, true),
                    _buildBasicInfoItem('应急专家', disposalModeData.emergencyExpert, true),
                    _buildBasicInfoItem('接警时间', disposalModeData.accidentReceptionTime, false),
                    _buildBasicInfoItem('事故概况', disposalModeData.accidentGeneral, false),
                    Container(
                      padding: EdgeInsets.symmetric(horizontal: 37.w, vertical: 6.w),
                      alignment: Alignment.centerLeft,
                      child: Text(
                        '事故现场照片',
                        style: TextStyle(
                          fontSize: 14.sp,
                          color: const Color(0xFF7C7D93),
                        ),
                      ),
                    ),
                    Container(
                      margin: EdgeInsets.symmetric(horizontal: 37.w, vertical: 6.w),
                      alignment: Alignment.centerLeft,
                      child: Wrap(
                        spacing: 10.w,
                        runSpacing: 10.w,
                        children: item.accidentScenePhotos.map(_buildPicItem).toList(),
                      ),
                    ),
                    AppGaps.vGap16,
                    const CustomGradientLine(),
                    AppGaps.vGap16,
                    _buildBasicInfoItem('总指挥', disposalModeData.commanderInChief, true,width: 150.w),
                    _buildBasicInfoItem('副总指挥', disposalModeData.deputyCommander, true,width: 150.w),
                    _buildBasicInfoItem('现场指挥长', disposalModeData.onSiteCommander, true,width: 150.w),
                    _buildBasicInfoItem('现场副指挥长', disposalModeData.deputyOnSiteCommander, true,width: 150.w),
                    _buildBasicInfoItem('现场响应小组', disposalModeData.onSiteTeam, true,width: 150.w),
                    _buildBasicInfoItem('现场处置措施建议', disposalModeData.liveHandelAdvice, true,width: 150.w),
                    _buildBasicInfoItem('个体防护措施建议', disposalModeData.instanceProtectAdvice, true,width: 150.w),
                    _buildBasicInfoItem('其他事项', disposalModeData.anotherAttention, false,width: 150.w),
                    AppGaps.vGap28,
                  ],
                ),
              ),
            ),
          ],
      ),
    );
  }

  Widget _buildPicItem(String url) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(8),
      child: SizedBox(
        height: 77.w,
        width: 77.w,
        child: Image.network(
          url.startsWith('http') ? url : Interface.fileUrl + url ,
          fit: BoxFit.cover,),
      ),
    ).clickShowLarge(
      url:  url.startsWith('http') ? url : Interface.fileUrl + url ,
    );
  }

  /// 基本信息
  Widget _buildBasicInfoItem(
      String key,
      String val,
      bool isRequired, {
        bool isPic = false,
        double? width,
      }) {
    return Padding(
      padding: EdgeInsets.symmetric(horizontal: 37.w, vertical: 6.w),
      child: Row(
        children: [
          SizedBox(
            width: width ?? 120.w,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                if (isRequired)
                  Text(
                    '*',
                    style: TextStyle(
                      fontSize: 13.sp,
                      color: Colors.red,
                    ),
                    textAlign: TextAlign.center,
                  ),
                Text(
                  key,
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: const Color(0xFF7C7D93),
                  ),
                )
              ],
            ),
          ),
          isPic
              ? SizedBox(
            height: 77.w,
            width: 77.w,
            child: Image.network(val),
          )
              : Text(
            val,
            style: TextStyle(
              fontSize: 12.sp,
              color: const Color(0xFF495061),
            ),
          ),
        ],
      ),
    );
  }

}
